/*==============================================================================
**                                  LINDBOARD
**                      Proprietary - Copyright (C) 2014
**                      www.facebook.com/lindboard
**------------------------------------------------------------------------------
** Supported MCUs      : STM32F
** Supported Compilers : ARM GCC
**------------------------------------------------------------------------------
** File name         : timer.c
**
** Module name       : Hardware Timer
**
**
** Summary: 
**
**= History ====================================================================
** 00.00.01 24/1/2014 Cuong Chu
** - Creation
===============================================================================*/
/******************************************************************************/
/* INCLUSIONS                                                                 */
/******************************************************************************/
#include "TIM/Timer.h"
#include "TIM/Timer_Cfg.h"
/******************************************************************************/
/* DEFINITION OF TYPES                                                        */
/******************************************************************************/

/******************************************************************************/
/* DECLARATION OF VARIABLES and  OF MODULE                                    */
/* Declaration of local variables shall have the specifier STATIC             */
/******************************************************************************/
static __IO uint32_t SystemTick = 0;
static __IO uint32_t TimingDelay = 0;
/******************************************************************************/
/* DECLARATION INTERNAL OF MODULE                                             */
/******************************************************************************/
/***********************************************************
** Function Name: Timer_IncrementSystick()
** Description  : The function increment of systick
** Parameter    : None
** Return       : None
** Remark       : global
************************************************************/
static void Timer_IncrementSystick(void)
{
	SystemTick++;
}

/***********************************************************
** Function Name: Timer_DecrementTimingDelay()
** Description  : The function make delay microsecond
** Parameter    : None
** Return       : None
** Remark       : global
************************************************************/
static void Timer_DecrementTimingDelay(void)
{
	/* check TimingDelay is > 0 */
	if(TimingDelay)
	{
		/* decrement value */
		TimingDelay--;
	}
	else
	{
		/* do nothing */
	}
}

/******************************************************************************/
/* DECLARATION OF GLOBAL FUNCTIONS OF MODULE                                  */ 
/******************************************************************************/
/***********************************************************
** Function Name: Timer_SystemTickTimerInit()
** Description  : The function shall be init system tick timer
** Parameter    : None
** Return       : None
** Remark       : global
************************************************************/
void Timer_SystemTickTimerInit(void)
{
  /* Setup SysTick Timer for 1 msec interrupts */
  if (SysTick_Config(SystemCoreClock / 1000))  
  { 
        /* Capture error */
       __disable_irq();
        NVIC_SystemReset();
  }
  else
  {
      /* init success */
  }
}
/*******************************************************************************
** Function name: Timer_GetCurrentValue( void )
** Description  : The function gets current value of Timer register
** Parameter    : None
** Return value : iTimerTick - current value of timer
** Remarks      : global variables used, side effects
*******************************************************************************/
uint32_t Timer_GetCurrentValue
(
	void
)
{
  return SystemTick;
}
/***********************************************************
** Function Name: Timer_DelayMs()
** Description  : The function make delay minisecond
** Parameter    : None
** Return       : None
** Remark       : global
************************************************************/
__IO void Timer_DelayMs(uint32_t nTime)
{
	TimingDelay = nTime;
	while(TimingDelay);
}
/***********************************************************
** Function Name: Timer_DelayUs()
** Description  : The function make delay microsecond
** Parameter    : None
** Return       : None
** Remark       : global
************************************************************/
__IO void Timer_DelayUs(uint32_t nTime)
{
	nTime+=50;
  while(nTime--);
}
/***********************************************************
** Function Name: ()
** Description  : Hardware System Tick Handler event
** Parameter    : None
** Return       : None
** Remark       : global
************************************************************/
void SysTick_Handler(void)
{
  Timer_IncrementSystick();
  Timer_DecrementTimingDelay();
}
/*******************************************************************************/
/*                      End of File                                            */
/*******************************************************************************/
